﻿CREATE PROCEDURE [acms].[Post_GetCurrentVersions]
	@PostGuid uniqueidentifier
AS
	declare @tmp DateTime
	declare @tmpTable table (Id int, SiteVersion DateTime)
	set @tmp = [acms].[GetPublishDate](getutcdate())
	
	insert into @tmpTable
		(Id, SiteVersion)
	select a.Id, Min(PublishDate)
	From acms.Post_View a
		inner join acms.PostPositions b on a.Id = b.PostId
	where a.PostGuid = @PostGuid
		and isNull(b.PublishDate, '9999/12/31') >= @tmp
	group by a.Id

	SELECT a.*, b.ParentPath, c.SiteVersion
	From acms.Post_View a
		inner join acms.PostPositions b on a.Id = b.PostId
		inner join @tmpTable c on a.Id = c.Id and isNull(b.PublishDate, '9999/12/31') = isNull(c.SiteVersion, '9999/12/31')
	where a.PostGuid = @PostGuid
		and isNull(b.PublishDate, '9999/12/31') >= @tmp
	order by isNull(c.SiteVersion, '9999/12/31')

RETURN 0;